/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.importjewelry;

import com.kmcsoft.goldshop.generic.service.ShopGenericService;
import com.kmcsoft.goldshop.importjewelry.service.ImportJewelryService;
import com.kmcsoft.goldshop.model.master.ProductJewelry;
import com.kmcsoft.goldshop.model.scm.ImportJewelryDetail;
import com.kmcsoft.goldshop.model.scm.ImportJewerlyOrder;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;

/**
 *
 * @author huyvu
 */
public class OrderInformation extends GenericForwardComposer {

    @Autowired
    private ImportJewelryService importJewelryService;
    @Autowired
    private ShopGenericService shopGenericService;
    private String style;
    private double totalWeight;
    private double totalWageCost;
    private double totalWagePrice;
    private String code;
    private double totalAmount;
    private NumberFormat numberFormat;
    private List<HashMap<Object, String>> amounts;
    private AnnotateDataBinder binder;

    public List<HashMap<Object, String>> getAmounts() {
        return amounts;
    }

    public String getCode() {
        return code;
    }

    public String getStyle() {
        return style;
    }

    public String getTotalAmount() {
        return numberFormat.format(totalAmount);
    }

    public String getTotalWageCost() {
        return numberFormat.format(totalWageCost);
    }

    public String getTotalWagePrice() {
        return numberFormat.format(totalWagePrice);
    }

    public String getTotalWeight() {
        return numberFormat.format(totalWeight);
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        numberFormat = NumberFormat.getInstance();
        self.setAttribute("modal", this);
        ImportJewerlyOrder order = shopGenericService.getImportJewerlyOrderById_DetachDetail(((Long) arg.get("importOrderId")));
        code = "" + order.getCode();
        totalAmount = order.getDetails().size();
        totalWeight = 0;
        totalWageCost = 0;
        totalWagePrice = 0;
        amounts = new ArrayList<HashMap<Object, String>>();
        for (ImportJewelryDetail detail : order.getDetails()) {
            ProductJewelry jewelry = importJewelryService.getProductJewelryById(detail.getProduct().getId());
            if (style == null) {
                style = jewelry.getStyle().getName();
            }
            totalWeight += jewelry.getTotalWeight();
            totalWageCost += jewelry.getWageCost();
            totalWagePrice += jewelry.getWagePrice();
            HashMap<Object, String> map = new HashMap<Object, String>();
            boolean flag = true;
            for (int i = 0; i < amounts.size(); i++) {
                HashMap<Object, String> hashMap = amounts.get(i);
                if (hashMap.get("type").equals(jewelry.getProductGold().getName())) {
                    hashMap.put("amount", "" + (Integer.parseInt(hashMap.get("amount")) + 1));
                    flag = false;
                    break;
                }
            }
            if (flag) {
                map.put("type", jewelry.getProductGold().getName());
                map.put("amount", "1");
                amounts.add(map);
            }

        }
        binder = new AnnotateDataBinder(self);
        self.setAttribute("binder", binder);
        binder.loadAll();
    }

    public void onDetach() {
        self.detach();
    }
}
